package editor.tester;

import org.jwalk.Channels;
import org.jwalk.ExecutionException;
import org.jwalk.GeneratorException;
import org.jwalk.JWalker;
import org.jwalk.LoaderException;
import org.jwalk.PermissionException;
import org.jwalk.Settings;
import org.jwalk.SettingsException;

import editor.Context;
import editor.Mode;
import editor.manager.WorkSpaceManager;
import editor.project.ProjectFile;

/**
 * Runs the JWalk Tester in a separate thread
 * 
 * @author Mark Dessain
 */
public class JWalkRunner implements Runnable 
{
	private ProjectFile file;
	
	/**
	 * @param file which is been tested
	 */
	public JWalkRunner(ProjectFile file)
	{
		this.file = file;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() 
	{
	
		// Starts Question Mode
		Context.switchMode(Mode.QUESTION);
		
		JWalker walker = Context.getJwalker();
		Channels channels = walker.getChannels();
		Settings settings = walker.getSettings();
		
		System.out.println("C" + 	settings.getCustomGenerators());
		
		channels.addQuestionListener(new QuestionResponseListener(file));
		channels.addReportListener(new ReportPublisher());
				
		
		ExitSecurityManager exitManager = new ExitSecurityManager(); 
		System.setSecurityManager(exitManager);

		try {
			WorkSpaceManager.testingManager.removeAll();
		
			if(Context.getSideBarManager().getProjectManager().compileProject(file.getProject()))
			{
				settings.setGeneratorDirectory(file.getProject().getDirectory("src"));
				settings.setTestClassDirectory(file.getProject().getDirectory("bin"));
				settings.setOracleDirectory(file.getProject().getDirectory("oracles"));
	
				settings.setTestClass(file.getClassFile());
				
				Context.getSideBarManager().getTestSettingsPanel().saveSettings();
				
				System.out.println("B" + 	settings.getCustomGenerators());
				
				Context.switchMode(Mode.QUESTION);
				
				System.out.println("Start exeute");
				walker.execute();
				System.out.println("Done exeute");
				
				// Ends Question Mode, and moves over to Testing Mode
				Context.switchMode(Mode.TESTING);
			}
			
		} catch (LoaderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
			Context.switchMode(Mode.CODING);
		} catch (PermissionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
			Context.switchMode(Mode.CODING);
		} catch (GeneratorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
			Context.switchMode(Mode.CODING);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
			Context.switchMode(Mode.CODING);
		} catch (SettingsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
			Context.switchMode(Mode.CODING);
		}
		catch (SecurityException e) {      
			e.printStackTrace();
	        System.out.println("Intercepted System.exit()");             
		}

		System.out.println("Done testing");
	}
}
